package com.shanzmoo.merchant.manage.api.manager.product;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shanzmoo.base.bean.PageRespBase;
import com.shanzmoo.base.bean.SHttpResponse;
import com.shanzmoo.base.util.CollectionUtil;
import com.shanzmoo.core.cache.CurrentUserCache;
import com.shanzmoo.db.module.product.domain.dto.BrandSaveDto;
import com.shanzmoo.db.module.product.domain.query.BrandQueryVO;
import com.shanzmoo.db.module.product.domain.vo.BrandVO;
import com.shanzmoo.db.module.product.domain.vo.UnitVO;
import com.shanzmoo.db.module.product.entity.ProductBrandEntity;
import com.shanzmoo.db.module.product.entity.ProductUnitEntity;
import com.shanzmoo.db.module.product.service.IProductBrandService;
import com.shanzmoo.merchant.manage.api.util.SecurityUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 商品品牌manager
 * @author xueqz
 */
@Component
@RequiredArgsConstructor
public class ProductBrandManager {

    @Autowired
    private IProductBrandService productBrandService;

    public SHttpResponse<PageRespBase<BrandVO>> brandList(BrandQueryVO brandQueryVO) {
        // 开启分页
        Page page = productBrandService.page(brandQueryVO.page(), new QueryWrapper<ProductBrandEntity>()
                .eq("deleted", false));
        List<BrandVO> reList = CollectionUtil.convert(page.getRecords(), BrandVO.class);
        PageRespBase<BrandVO> re = PageRespBase.create(page,reList);
        return SHttpResponse.ok(re);
    }

    public SHttpResponse<Boolean> addBrand(BrandSaveDto brandSaveDto) {
        ProductBrandEntity data = brandSaveDto.convert(ProductBrandEntity.class);
        data.setCreateUser(SecurityUtil.getCurrentUserId());
        Assert.isTrue(productBrandService.save(data),"创建商品单位品牌失败");
        return SHttpResponse.ok();
    }

    public SHttpResponse<Boolean> delBrandById(Integer brandId) {
        ProductBrandEntity data = new ProductBrandEntity();
        data.setId(brandId);
        data.setDeleted(Boolean.TRUE);
        data.setUpdateUser(SecurityUtil.getCurrentUserId());
        Assert.isTrue(productBrandService.updateById(data),"更新商品品牌失败");
        return SHttpResponse.ok();
    }
}
